<template>
  <div>
    <!--    面包屑区域-->
    <!-- 面包屑导航区域-->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home/users' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--    添加按钮-->
    <el-card>
      <el-row style="border-top:none">
        <el-col>
          <el-button type="primary" @click="addRolesDialog">添加角色</el-button>
        </el-col>
      </el-row>
      <!--表格区域-->
      <el-table :data="rolesList" border highlight-current-row style="width: 100%" :cell-style="{padding:'0'}" :row-style="{padding:'0'}">
        <el-table-column label="" type="expand">
          <template #default="scope">
            <!--一级权限-->
            <el-row v-for="(item1,i1) in scope.row.children" :key="item1.id"
                    :class="['bottom',i1 === 0? 'elTop':'','vcenter']">
              <el-col :span="5">
                <el-tag>{{ item1.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限和三级权限-->
              <el-col :span="19">
                <el-row v-for="(item2,i2) in item1.children" :key="item2.id" class="vcenter">
                  <el-col :span="6">
                    <el-tag type="success">{{ item2.authName }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级权限-->
                  <el-col :span="18">
                    <el-tag v-for="(item3,i3) in item2.children" :key="item3.id" closable type="warning"
                            @close="removeTag(scope.row.id,item3.id)">{{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>


            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index">
        </el-table-column>
        <el-table-column label="角色名称" prop="roleName">
        </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" @click="updateRoles(scope.row)">编辑</el-button>
            <el-button type="danger" @click="removeRoles(scope.row)">删除</el-button>
            <el-button type="warning" @click="showSetRightDialog(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    对话框添加角色-->
      <el-dialog v-model="dialogVisible" title="添加角色" width="30%" @close="addDialogClose">
        <el-form ref="addFormRolesRef" :model="formRoles" :rules="formRolesRules" hide-required-asterisk
                 label-position="left" label-width="80px">
          <el-form-item label="角色名称" prop="roleName">
            <el-input v-model="formRoles.roleName"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" prop="roleDesc">
            <el-input v-model="formRoles.roleDesc"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addFormRoles">确 定</el-button>
    </span>
        </template>
      </el-dialog>
    </el-card>
    <!--    分配权限对话框-->
    <Dialog ref="diaLog" >
    </Dialog>
<!--    编辑角色列表的信息-->
    <UpdateDialog  ref="updateDialog"></UpdateDialog>
  </div>
</template>

<script>
import Dialog from "./Dialog";
import UpdateDialog from "./UpdateDialog";
import {request} from "../../../network/axios";
import {ElMessage} from "element-plus";

export default {
  name: "Roles",
  created() {
    this.getRolesList()
  },
  data() {
    return {
      rolesList: [],
      dialogVisible: false,
      formRoles: {
        roleName: '',
        roleDesc: '',
        nodes:'',
      },
      //验证规则
      formRolesRules: {
        roleName: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 2, max: 6, message: '请输入2-4位字符串', trigger: 'blur'}
        ],
        roleDesc: [
          {mix: 2, max: 10, message: '请输入2-10位字符串', trigger: 'blur'}
        ]
      },
      rolesId:Number
    }
  },
  methods: {
    //获取角色列表
    getRolesList() {
      request({
        url: 'roles',
      }).then(res => {
        this.rolesList = res.data.data
      })
    },
    //添加角色
    addRolesDialog() {
      this.dialogVisible = !this.dialogVisible
    },
    //监听对话框关闭事件
    addDialogClose() {
      this.$refs.addFormRolesRef.resetFields()
    },
    //添加用户
    addFormRoles() {
      this.$refs.addFormRolesRef.validate(valid => {
        if (!valid) return
        request({
          url: 'roles',
          method: 'post',
          data: this.formRoles
        }).then(res => {
          if (res.status !== 200) return ElMessage.error("添加失败")
          ElMessage.success("添加成功")
          //关闭对话框
          this.dialogVisible = false
          //重新刷新
          this.getRolesList()
        })
      })
    },
    //根据id删除行,val为对象
    removeRoles(val) {
      //弹框确认
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ElMessage.success("删除成功")
        request({
          url: `roles/${val.id}`,
          data: val.id,
          method: 'delete'
        }).then((res) => {
          console.log(res);
          this.getRolesList()
        })
      }).catch(() => {
            ElMessage.info("删除失败")
          }
      )
    },
    //根据权限id和角色id删除tag
    removeTag(roleId, rightId) {
      request({
        url: `roles/${roleId}/rights/${rightId}`,
        method: 'delete',
        data: {roleId, rightId}
      }).then(res => {
        if (res.status !== 200) ElMessage.error("删除权限失败")
        ElMessage.success("删除权限成功")
        //重新刷新页面
        this.getRolesList()
      })
    },
    // 展示分配权限对话框
    showSetRightDialog(role) {
      this.$refs.diaLog.tagDialogVisible = !this.$refs.diaLog.tagDialogVisible
      this.nodes = role
      this.rolesId = role.id
    },
    updateRoles(val){
    this.$refs.updateDialog.updateDialogVisible= !this.$refs.updateDialog.updateDialogVisible
      this.$refs.updateDialog.id = val.id
      if(this.$refs.updateDialog.updateDialogVisible) return this.getRolesList()
  },
  },
  components: {
    Dialog,
    UpdateDialog
  }
}
</script>

<style scoped src="../../../assets/css/Roles.css">

</style>